package com.example.AIstudy.dao;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.example.AIstudy.DTO.RedoRecordDTO;
import com.example.AIstudy.domain.ProSet;
import com.example.AIstudy.domain.RecordDetail;
import com.example.AIstudy.domain.RedoRecord;
import org.apache.ibatis.annotations.*;

import java.sql.Timestamp;
import java.util.List;

@Mapper
public interface RedoRecordDao extends BaseMapper<RedoRecord> {

    //查询所有错题最后一次做题的结果
    @Select("SELECT t1.id as redoID,t1.redoTime,t1.redoRight,t1.redoScore,t1.falseReason,t3.*\n" +
            "FROM redorecord t1\n" +
            "JOIN (\n" +
            "    SELECT cuotiID, MAX(redoTime) AS max_value\n" +
            "    FROM redorecord\n" +
            "    GROUP BY cuotiID\n" +
            ") t2 ON t1.cuotiID = t2.cuotiID AND t1.redoTime = t2.max_value \n" +
            "Right JOIN cuotiji t3 ON t1.cuotiID = t3.id;")
    public List<RedoRecordDTO> queryLastTimeRedoDetail();

    @Select("SELECT p.id as redoID,p.redoTime,p.redoRight,p.redoScore,p.falseReason,k.* FROM redorecord as p,cuotiji as k ${ew.customSqlSegment} and p.cuotiID=k.id")
    public IPage<RedoRecordDTO> queryRedoRecord(IPage<RedoRecordDTO> page, @Param(Constants.WRAPPER) QueryWrapper<RedoRecordDTO> wrapper);

    //插入一条做题的记录
    @Insert("INSERT INTO redorecord(cuotiID,redoTime,redoAnswer,redoRight,redoScore,redoAIComment,redoUserComment,redoTranslationID,redoCompositionID,falseReason,redoSonProID) " +
            "VALUES (#{cuotiID},#{redoTime},#{redoAnswer},#{redoRight},#{redoScore},#{redoAIComment},#{redoUserComment},#{redoTranslationID},#{redoCompositionID},#{falseReason},#{redoSonProID})")
    @SelectKey(statement = "select last_insert_id()", keyProperty = "id",
            keyColumn = "id", resultType = Integer.class, before = false)
    void insertRedoRecord(RedoRecord record);
}
